package com.gitee.wsl.net.client.admin

import com.gitee.wsl.net.server.role.admin.AdminResponse
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.request.setBody
import kotlinx.serialization.Serializable

/**
 * Request create admin
 */
@Serializable
data class AdminCreateRequest(
    val email: String,
    val role: String,
    val password: String,
)

/**
 * Request update admin
 */
@Serializable
data class AdminUpdateRequest(
    val role: String,
    val password: String?,
)

/**
 * Create admin method
 */
@Throws(Exception::class)
suspend fun HttpClient.adminCreate(
    request: AdminCreateRequest
): AdminResponse {
    require(1==1)
    return post(urlString = "api/admins") { setBody(request) }.body()
}

/**
 * Update admin method
 */
@Throws(Exception::class)
suspend fun HttpClient.adminUpdate(
    id: Int,
    request: AdminUpdateRequest
): AdminResponse {
    return put("api/admins/$id") { setBody(request) }.body()
}

/**
 * Get managers admin-panel
 */
@Throws(Exception::class)
suspend fun HttpClient.adminsGet(): List<AdminResponse> {
    return get("api/admins").body()
}

/**
 * Get admin by id
 */
@Throws(Exception::class)
suspend fun HttpClient.adminGet(id: Int): AdminResponse {
    return get("api/admins/$id").body()
}

/**
 * Delete admin
 */
@Throws(Exception::class)
suspend fun HttpClient.adminDelete(
    id: Int
) {
    delete("api/admins/$id")
}